$(function(){
	$("#box-tabs").tabs();
	getUserPageData();
});

var xmlHttp=GetXmlHttpObject();
var empty;
var mydialog;
var uname="";

function edit_stateChanged(data) 
{  
	var select='';
	$.each(data["data"],function(key,value){
		var onelist="<option value='"+value["role_id"]+"' >"+value["role_name"]+"</option>";
		select=select+onelist;
	});
	var view="<form action='#' method='post'><ul><li><h3>用戶："+uname+"</h3></li><li><h3>角色：<select id='select'>"+select+"</select></h3></li></ul></form>";
	mydialog.content(view);

}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
	// Firefox, Opera 8.0+, Safari
	xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
	// Internet Explorer
 try
  {
	 xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
	 xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}

function edit(id,name){
	if (xmlHttp==null)
    {
		alert ("Browser does not support HTTP Request")
		return false;
    }else{
    	uname=name;
    	var staff_id=id;
    	mydialog=art.dialog({
    		title:'角色修改',
    		content:empty,
    		lock:true,
    		ok:function(){
    			var role_id=document.getElementById('select').value;
    	    	var url="edit_save";
    	    	url=url+"?role_id="+role_id+"&&staff_id="+staff_id;
    	    	xmlHttp.onreadystatechange=editsave_stateChanged;
    	    	xmlHttp.open("GET",url,true);
    	    	xmlHttp.send(null);
    		},
    		cancel:function(){},
    		width:300
    	});
    	$.ajax({
    		type:'post',
    		url:'editor',
    		dataType:'json',
    		data:{id:id},
    		success:edit_stateChanged
    	});
    }
}

function deleteu(id){
	if (xmlHttp==null)
    {
		alert ("Browser does not support HTTP Request")
		return false;
    }else{
    	art.dialog({
    		title:'刪除確認',
    		content:'您確定要刪除該用戶嗎？',
    		lock:true,
    		ok:function(){
    			var url="deleteu";
    			url=url+"?id="+id;
    			xmlHttp.onreadystatechange=del_stateChanged;
    			xmlHttp.open("GET",url,true);
    			xmlHttp.send(null);
    		},
    		cancel:function(){}
    	});
    }
}

function del_stateChanged(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		var msg=xmlHttp.responseText;
		var info=eval('('+msg+')');
		if(info.status===1){
			art.dialog({
				title:'提示',
				content:"刪除成功",
				lock:true,
				ok:function(){
					location.reload();
				},
				width:300
			});
		}else{
			art.dialog({
				title:'提示',
				content:"刪除失敗",
				lock:true,
				ok:function(){},
				width:300
			});
		}
	}
}

function editsave_stateChanged(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		var msg=xmlHttp.responseText;
		var info=eval('('+msg+')');
		if(info.status===1){
			art.dialog({
				title:'提示',
				content:"修改成功",
				lock:true,
				ok:function(){
					location.reload();
				},
				width:300
			});
		}else{
			art.dialog({
				title:'提示',
				content:"修改失敗",
				lock:true,
				ok:function(){},
				width:300
			});
		}
	}
}

function getUserPageData(){
	$.ajax({
		url:'authority',
		type:'post',
		data:{operator:'14'},
		dataType:'html',
		success:getUserPageData_status
	});
}

function getUserPageData_status(data){
	$('#box-messages').html(data);
}

function getRolePageData(){
	$.ajax({
		url:'auth_role',
		type:'post',
		data:{operator:'15'},
		dataType:'html',
		success:getRolePageData_status
	});
}

function getRolePageData_status(data){
	$('#box-other').html(data);
}
